#pragma once

#include <iostream>
#include <string>
#include <strings.h>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace Client
{
    using namespace std;

    class udpClient
    {
    public:
        udpClient(const string &serverip, const uint16_t &serverport)
            : _serverip(serverip), _serverport(serverport), _sockfd(-1), _quit(false)
        {
        }
        void initClient()
        {
            // 创建socket
            _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (_sockfd == -1)
            {
                cerr << "socket error: " << errno << " : " << strerror(errno) << endl;
                exit(2);
            }
            cout << "socket success: " << " : " << _sockfd << endl;
            // bind OS替我们做
        }
        void run()
        {
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_addr.s_addr = inet_addr(_serverip.c_str());
            server.sin_port = htons(_serverport);
            while (1)
            {
                cout << "Please make a chance betwen  1:传文件  &&  2:传文字  &&  3:退出 # ";
                int chance;
                cin >> chance;
                switch (chance)
                {
                case 1:
                {
                    int fd = open("./data.txt", O_RDWR | O_CREAT, 0666);
                    if(fd == -1) { cerr << "open error: " << errno << " : " << strerror(errno) << endl; exit(-1); }   
                    int len = 0;
                    char message[1024];
                    while(len = read(fd, (void*)message, sizeof(message)) > 0) 
                    {
                        len--;
                        write(fd, (void*)message, len);
                    }
                    close(fd);
                    sendto(_sockfd, message, sizeof(message), 0, (struct sockaddr *)&server, sizeof(server));
                    cout << " send success " << endl;
                    break;
                }
                case 2:
                {
                    string message;
                    int cnt = 1;
                    while (!_quit)
                    {
                        if (cnt % 5 == 0)
                        {
                            int _quit;
                            cout << " 1 for quit && 2 for continue  # ";
                            cin >> _quit;
                            if (_quit == 1)
                                break;
                        }
                        cout << "Please Enter# ";
                        cin >> message;
                        sendto(_sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));
                        ++cnt;
                    }
                    break;
                }
                case 3:
                {
                    cout << "quit success" << endl;
                    exit(0);
                }
                default:
                {
                    cout << "please insert again : 1 or 2 " << endl;
                    break;
                }
                }
            }
        }
        ~udpClient()
        {
        }

    private:
        int _sockfd;
        string _serverip;
        uint16_t _serverport;
        bool _quit;
    };
} // namespace Client